package com.seeyou.lake.ui.edt

import android.app.Activity
import android.content.Intent
import com.seeyou.lake.base.MyBaseActivity
import com.seeyou.lake.base.myBaseViewModel
import com.seeyou.lake.base.EventBusVo
import com.seeyou.lake.databinding.ActivityEditBinding
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class EditActivityMy : MyBaseActivity<ActivityEditBinding, myBaseViewModel>() {

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, EditActivityMy::class.java)
            activity.startActivity(intent)
        }
    }

    override fun myViewModel(): myBaseViewModel {
        return myBaseViewModel()
    }

    override fun yourViewBing(): ActivityEditBinding {
        return ActivityEditBinding.inflate(layoutInflater)
    }

    override fun init() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
        mViewBinding.ivEdtBack.setOnClickListener { finish() }
        mViewBinding.tvEdtNext.setOnClickListener {
            if (mViewBinding.edtInput.text.isEmpty()){
                return@setOnClickListener
            }
            PreviewPageActivityMy.open(
                this,
                mViewBinding.edtInput.text.toString()
            )
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun finishEvent(event: EventBusVo?) {
        if (event == null) {
            return
        }

        if (event.type == 1) {
            finish()
        }
    }
}